# Maintainer: Rui Abreu Ferreira <raf-ep@gmx.com>

_realname=mpack
pkgbase=mingw-w64-lua-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-lua51-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-lua-${_realname}")
pkgver=1.0.10
pkgrel=2
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://github.com/libmpack/libmpack'
license=('MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-lua51"
             "${MINGW_PACKAGE_PREFIX}-lua"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-libmpack"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/libmpack/libmpack-lua/archive/${pkgver}.tar.gz")
sha256sums=('b58a25a0b48e1ec584beee6c80e0e31182dad7a45005ce58e88e6cfff3a3f3e4')

build() {
  cd libmpack-lua-${pkgver}
  ${CC} -O2 -fPIC -DMPACK_USE_SYSTEM $(pkg-config --cflags lua) -shared -o mpack.dll lmpack.c $(pkg-config --libs lua) -lmpack
  ${CC} -O2 -fPIC -DMPACK_USE_SYSTEM $(pkg-config --cflags lua5.1) -shared -o mpack.dll.5.1 lmpack.c $(pkg-config --libs lua5.1) -lmpack
}

package_lua-mpack() {
  pkgdesc='Msgpack serialization library for Lua (mingw-w64)'
  depends=("${MINGW_PACKAGE_PREFIX}-lua" "${MINGW_PACKAGE_PREFIX}-libmpack")

  cd libmpack-lua-${pkgver}
  local luaver=$(pkg-config lua --modversion | sed -r 's/^([0-9]+[.][0-9]+)[.][0-9]+$/\1/')
  install -Dm755 mpack.dll "${pkgdir}${MINGW_PREFIX}/lib/lua/${luaver}/mpack.dll"
  install -Dm644 LICENSE-MIT "${pkgdir}${MINGW_PREFIX}/share/licenses/lua-lmpack/LICENSE"
}

package_lua51-mpack() {
  pkgdesc='Msgpack serialization library for Lua 5.1 (mingw-w64)'
  depends=("${MINGW_PACKAGE_PREFIX}-lua51" "${MINGW_PACKAGE_PREFIX}-libmpack")

  cd libmpack-lua-${pkgver}
  install -Dm755 mpack.dll.5.1 "${pkgdir}${MINGW_PREFIX}/lib/lua/5.1/mpack.dll"
  install -Dm644 LICENSE-MIT "${pkgdir}${MINGW_PREFIX}/share/licenses/lua51-mpack/LICENSE"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
